package com.google.code.ptrends.entities;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.google.code.ptrends.common.entities.Category;
import com.google.code.ptrends.common.entities.ItemClass;
import com.google.code.ptrends.common.entities.Manufacturer;
import com.google.code.ptrends.common.entities.Property;

public class ETLItem {
    private float price;
    private Category category;
    private ItemClass itemClass;
    private String itemModel;
    private Manufacturer manufacturer;
    private List<Property> parameters;
    private Date priceDate;
    private int supplierID;

    // private Session session;

    public ETLItem() {
	parameters = new ArrayList<Property>();
    }

    public float getPrice() {
	return price;
    }

    public void setPrice(final float price) {
	if (price < 0) {
	    throw new IllegalArgumentException("Illegal negative price");
	}

	this.price = price;
    }

    public Category getCategory() {
	return category;
    }

    public void setCategory(final Category category) {
	if (category == null) {
	    throw new IllegalArgumentException("Illegal null reference category");
	}

	this.category = category;
    }

    public ItemClass getItemClass() {
	return itemClass;
    }

    public void setItemClass(final ItemClass itemClass) {
	if (itemClass == null) {
	    throw new IllegalArgumentException("Illegal null reference item class");
	}

	this.itemClass = itemClass;
    }

    public int getSupplierID() {
	return supplierID;
    }

    public void setSupplierID(int supplierID) {
	this.supplierID = supplierID;
    }

    public String getItemModel() {
	return itemModel;
    }

    public void setItemModel(final String itemModel) {
	if (itemModel == null) {
	    throw new IllegalArgumentException("Illegal null reference item model");
	}

	if ("".equals(itemModel)) {
	    throw new IllegalArgumentException("Illegal empty item model");
	}

	this.itemModel = itemModel;
    }

    public Manufacturer getManufacturer() {
	return manufacturer;
    }

    public void setManufacturer(final Manufacturer manufacturer) {
	if (manufacturer == null) {
	    throw new IllegalArgumentException("Illegal null reference manufacturer");
	}

	this.manufacturer = manufacturer;
    }

    public List<Property> getParameters() {
	return parameters;
    }

    public void setParameters(final List<Property> parameters) {
	if (validateParameters(parameters)) {
	    this.parameters = new ArrayList<Property>(parameters);
	} else {
	    throw new IllegalArgumentException(
		    "There are duplicates in parameters or parameters collection is null");
	}
    }

    public Date getPriceDate() {
	Date cloned = null;
	if (priceDate != null) {
	    cloned = new Date(priceDate.getTime());
	}
	return cloned;
    }

    public void setPriceDate(final Date priceDate) {
	this.priceDate = priceDate == null ? null : new Date(priceDate.getTime());
    }

    /*
     * public Session getSession() { return session; }
     * 
     * public void setSession(final Session session) { if (session == null)
     * throw new IllegalArgumentException("Illegal null reference session");
     * 
     * this.session = session; }
     */
    /**
     * Validates that there is no any pair of parameters in list with the same
     * names
     * 
     * @param parameters
     */
    private boolean validateParameters(final List<Property> parameters) {
	boolean result = false;
	if (parameters != null) {
	    final Set<String> ids = new HashSet<String>(parameters.size());
	    for (int i = 0; i < parameters.size(); ++i) {
		final Property property = parameters.get(i);
		if (!validateProperty(property)) {
		    result = false;
		    break;
		}
		ids.add(property.getName());
	    }

	    result = ids.size() == parameters.size(); // no duplicates by
						      // parameter name
	}

	return result;
    }

    private boolean validateProperty(final Property property) {
	boolean result = false;

	if (property != null) {
	    result = StringUtils.isNotBlank(property.getName());
	    result &= StringUtils.isNotBlank(property.getValue());
	}

	return result;
    }

}